package com.model.action.branch;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.Gson;
import com.model.action.BaseAction;
import com.model.service.academy.AcademyService;
import com.model.service.admin.AdminService;
import com.model.service.branch.BranchService;
import com.model.service.student.StudentService;
import com.model.util.PageList;
import com.model.vo.Academy;
import com.model.vo.Admin;
import com.model.vo.Branch;
import com.model.vo.GradeClass;
import com.model.vo.Student;

@SuppressWarnings("serial")
public class BranchAction extends BaseAction {

	 
		/*实体参数 声明
		 **********************************************/
	     Branch branch;   //实体类
		@Autowired
		BranchService branchService;  //业务层
		String resultView;   //视图页面
		PageList<Branch> pageList;
		/*相关方法
		 * ********************************************/
	    @Override
	    public String execute() throws Exception {
	    	
	    	return INPUT;
	    }
		/**
		 * 保存
		 * @return
		 */
		public String save(){
			if(branch!=null){
				branchService.save(branch);
			}
			 resultView ="/admin/quarters_queryList.action?pageList.pageNo=1";
				return "update";
		}
		/**
		 * 更新
		 * @return
		 */
	    public String update(){
	    	if(branch!=null){
	    		branchService.update(branch);
			}
	    	 resultView ="/admin/branch_queryList.action?pageList.pageNo=1";
			return "update";
		}
	    /**
	     * 删除
	     * @return
	     */
	   public String delete(){
		   if(branch!=null){
			   branchService.delete(branch);
			}
		   resultView ="/admin/branch_queryList.action?pageList.pageNo=1";
			return "update";
		}
	   /**
	    * 分页查询
	    * @return
	    */
		public String queryList(){
			
	    	if(pageList==null){
	    		pageList = new PageList<Branch>();	
	    	}
	    	pageList.setPageSize(15);
	    		int allrow = branchService.getAllRowCount("from  Branch", null);
	    		pageList.setRowCount(allrow);
	    		System.out.println(pageList.getStartOfPage()+","+allrow+","+pageList.getEndRow());
	    		pageList.setDataList(branchService.queryForPage(
	    				pageList.getStartOfPage(), pageList.getEndRow(), "from Branch",null));
			
			return SUCCESS;
		}
	    
	   
	    /**
	     * 通过Id查询
	     * @return
	     */
	   public String queryById(){
		   if(branch==null){
			   branch = new Branch();
			}	
		   branch = branchService.QueryById(Branch.class,branch.getId());
			return "edit";
		}
	   
	   /***
	    * json查询
	    */
	   public void queryJsonList(){
		   if(pageList==null){
	    		pageList = new PageList<Branch>();	
	    	}
	    	pageList.setPageSize(15);
	    		int allrow = branchService.getAllRowCount("from  Branch", null);
	    		pageList.setRowCount(allrow);
	    		System.out.println(pageList.getStartOfPage()+","+allrow+","+pageList.getEndRow());
	    		pageList.setDataList(branchService.queryForPage(
	    				pageList.getStartOfPage(), pageList.getEndRow(), "from Branch",null));
	    		resultJson();
	   }
		/**
		 * 
		 * @param json
		 */
		private void resultJson() {
			try {
				HttpServletResponse response = ServletActionContext.getResponse();
				response.setContentType("text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				Gson g  = new Gson();
				String json = g.toJson(pageList);
		        PrintWriter pw = response.getWriter();
		        pw.print(json);
		        pw.flush();
		        pw.close();
			} catch (Exception e) {
				e.printStackTrace();
			}	

		}
		
		
	   /*相关参数注入
	  	 *********************************************/
		
		public String getResultView() {
			return resultView;
		}
		
		public void setResultView(String resultView) {
			this.resultView = resultView;
		}
		public Branch getBranch() {
			return branch;
		}
		public void setBranch(Branch branch) {
			this.branch = branch;
		}
		public BranchService getBranchService() {
			return branchService;
		}
		public void setBranchService(BranchService branchService) {
			this.branchService = branchService;
		}
		public PageList<Branch> getPageList() {
			return pageList;
		}
		public void setPageList(PageList<Branch> pageList) {
			this.pageList = pageList;
		}
		
		
			    
	}
